# coding:utf-8 

'''
powered by Goldgom
before use this tool you may need to run:

    pip install PyQt6             ||    pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PyQt6

**************************************
remember to use it with autotranslate.py && baidufanyi.py
'''

import autotranslate as translate
from PyQt6 import QtCore, QtWidgets 
import sys
import os
import re
class Ui_translateGUI(object):
    inputPath = ""
    outputPath = ""

    #路径字符

    inputPathButton = None
    outputPathButton = None
    startButton = None
    inputFileButton = None

    #按钮

    inputPathLabel = None
    outputPathLabel = None
    startLabel = None
    inputFileLabel = None
    proxyLabel = None

    #标签

    proxyline = None
    
    #输入框
    
    parent = None

    #父对象

    useFile = False

    #批量翻译标志

    def setupUi(self, parent):
        self.parent = parent
        #保存父对象
        self.inputPathButton = QtWidgets.QPushButton(parent)
        self.inputPathButton.setText("选择待翻译文档")
        self.inputPathButton.setGeometry(QtCore.QRect(0, 0, 100, 50))
        self.inputPathButton.clicked.connect(self.getInputPath)
        #
        self.outputPathButton = QtWidgets.QPushButton(parent)
        self.outputPathButton.setText("选择输出路径")
        self.outputPathButton.setGeometry(QtCore.QRect(0, 50, 100, 50))
        self.outputPathButton.clicked.connect(self.getOutputPath)
        #
        self.inputFileButton = QtWidgets.QPushButton(parent)
        self.inputFileButton.setText("批量翻译")
        self.inputFileButton.setGeometry(QtCore.QRect(0, 100, 100, 50))
        self.inputFileButton.clicked.connect(self.getInputFile)
        #
        self.startButton = QtWidgets.QPushButton(parent)
        self.startButton.setText("开始翻译")
        self.startButton.setGeometry(QtCore.QRect(0, 150, 100, 50))
        self.startButton.clicked.connect(self.startTranslate)
        #
        #按钮设置

        self.inputPathLabel = QtWidgets.QLabel(parent)
        self.inputPathLabel.setText("")
        self.inputPathLabel.setGeometry(QtCore.QRect(100, 0, 300, 50))
        #
        self.outputPathLabel = QtWidgets.QLabel(parent)
        self.outputPathLabel.setText("")
        self.outputPathLabel.setGeometry(QtCore.QRect(100, 50, 300, 50))
        #
        self.inputFileLabel = QtWidgets.QLabel(parent)
        self.inputFileLabel.setText("")
        self.inputFileLabel.setGeometry(QtCore.QRect(100, 100, 300, 50))
        #
        self.startLabel = QtWidgets.QLabel(parent)
        self.startLabel.setText("")
        self.startLabel.setGeometry(QtCore.QRect(100, 150, 300, 50))
        #
        self.proxyLabel = QtWidgets.QLabel(parent)
        self.proxyLabel.setText("代理地址")
        self.proxyLabel.setGeometry(QtCore.QRect(100, 200, 300, 50))
        self.repaintAllLabel()

        #
        #状态输出栏

        self.proxyline = QtWidgets.QTextEdit(parent)
        self.proxyline.setGeometry(QtCore.QRect(0, 200, 100, 50))
    def getInputPath(self):
        self.useFile = False
        self.inputPath = QtWidgets.QFileDialog.getOpenFileName(self.parent, "请选择待翻译文档路径", "./","pdf Files (*.pdf);;doc Files (*.doc);;docx Files (*.docx);;All Files (*)")
        self.inputPathLabel.setText(self.inputPath[0])
        self.inputFileLabel.setText("")
        self.repaintAllLabel()
    #
    def getInputFile(self):
        self.useFile = True
        self.inputPath = QtWidgets.QFileDialog.getExistingDirectory(self.parent, "请选择待翻译文档所在文件夹", "./")
        self.inputPathLabel.setText("")
        self.inputFileLabel.setText(self.inputPath)
        self.repaintAllLabel()
    #
    def getOutputPath(self):
        self.outputPath = QtWidgets.QFileDialog.getExistingDirectory(self.parent, "请选择输出路径", "./")
        self.outputPathLabel.setText(self.outputPath)
        self.repaintAllLabel()
    #
    def startTranslate(self):
        self.startLabel.setText("初始化翻译器...")
        self.repaintAllLabel()
        proxynum = self.proxyline.toPlainText()
        print("proxy: {}".format(proxynum))
        if proxynum == 0 :
            myproxy = None
        else:
            myproxy = {'http':'127.0.0.1:1080','https':'127.0.0.1:1080'}
        translate.init(url="com",proxy=myproxy)

        self.startLabel.setText("工作中...")
        self.repaintAllLabel()
        print("待翻译文档路径：{}".format(self.inputPath))
        print("输出文档路径：{}".format(self.outputPath))
        print("是否启用多文档翻译：{}".format(self.useFile))
        try:
            if self.useFile:
                files = os.listdir(self.inputPath)
                for file in files:
                    if ".pdf" not in file:
                        files.remove(file)
                num = len(files)
                print("工作队列：{}".format(files))
                print("总计：{}".format(num))
                if num == 0:
                    self.startLabel.setText("空闲中")
                    self.repaintAllLabel()
                    QtWidgets.QMessageBox.warning(self.parent, '警告','你指定的文件夹下无有效文件，请检查路径是否正确。')
                    return
                for i in range(0,num):
                    self.startLabel.setText("工作中...({}/{})".format(i,num))
                    self.repaintAllLabel()
                    translate.translatePDF("{}/{}".format(self.inputPath,files[i]),"{}/{}docx".format(self.outputPath,re.sub("[p|P][d|D][f|F]","",files[i])))
            else:
                translate.translatePDF(self.inputPath[0],"{}/translated.docx".format(self.outputPath))
        except:
            self.startLabel.setText("出现错误")
            self.repaintAllLabel()
            QtWidgets.QMessageBox.warning(self.parent, '错误','出现错误，可能是PDF格式损坏导致的，请检查所有PDF格式是否正确或参数是否异常。')
            return 
        QtWidgets.QMessageBox.information(self.parent, '翻译完成','翻译完成。')
        self.startLabel.setText("空闲中")
        self.repaintAllLabel()
    #
    def repaintAllLabel(self):
        self.inputPathLabel.repaint()
        self.outputPathLabel.repaint()
        self.startLabel.repaint()
        self.inputFileLabel.repaint()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWindows = QtWidgets.QWidget()
    mainWindows.setWindowTitle("翻译工具 powered by Goldgom")
    translateGUI = Ui_translateGUI()
    translateGUI.setupUi(mainWindows)
    mainWindows.show()
    sys.exit(app.exec())